﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAdminBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm13 : System.Web.UI.Page
    {
        static bool authorSelected;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
            }
            visibility(false, false, false);
            

        }

        private void visibility(bool gridAuthor, bool gridBook, bool _lblPopular)
        {
            gridMostPopularAuthor.Visible = gridAuthor;
            gridMostPopularBook.Visible = gridBook;      
            lblPopular.Visible = _lblPopular;

        
        }

        protected void btnBook_Click(object sender, EventArgs e)
        {

            DropDownList1.SelectedIndex = 0;
            DropDownList2.SelectedIndex = 0;
            int month;
            int year;
            authorSelected = false;
         
            DropDownList1.Visible = true;
            DropDownList2.Visible = true;

            if (DropDownList2.SelectedIndex != 0 && DropDownList1.SelectedIndex != 0)
            {
                month = Convert.ToInt32(DropDownList1.SelectedValue);
                year = Convert.ToInt32(DropDownList2.SelectedValue);
           
                    visibility(false, true, false);
                    gridMostPopularBook.DataSource = MostPopularBL.popularBookList(month, year);
                    gridMostPopularBook.EmptyDataText = "No data available";
                    gridMostPopularBook.DataBind();
                
   
                
            }

        }

        protected void btnAuthor_Click(object sender, EventArgs e)
        {
            DropDownList1.SelectedIndex = 0;
            DropDownList2.SelectedIndex = 0;
            int month;
            int year;
            authorSelected = true;
   
            DropDownList1.Visible = true;
            DropDownList2.Visible = true;

            if (DropDownList2.SelectedIndex != 0 && DropDownList1.SelectedIndex != 0)
            {
                month = Convert.ToInt32(DropDownList1.SelectedValue);
                year = Convert.ToInt32(DropDownList2.SelectedValue);

                visibility(true, false, false);
                gridMostPopularAuthor.DataSource = MostPopularBL.popularAuthorList(month, year);
                gridMostPopularBook.EmptyDataText = "No data available";
                gridMostPopularBook.DataBind();



            }
        }

 


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int month;
            int year;
            if (DropDownList2.SelectedIndex != 0 && DropDownList1.SelectedIndex != 0)
            {
                month = Convert.ToInt32(DropDownList1.SelectedValue);
                year = Convert.ToInt32(DropDownList2.SelectedValue);
                if (authorSelected == false)
                {
                    visibility(false, true, false);
                    gridMostPopularBook.DataSource = MostPopularBL.popularBookList(month, year);
                    gridMostPopularBook.EmptyDataText = "No data available";
                    gridMostPopularBook.DataBind();
                }
                else if (authorSelected == true)
                {
                    visibility(true, false, false);
                    gridMostPopularAuthor.DataSource = MostPopularBL.popularAuthorList(month, year);
                    gridMostPopularAuthor.EmptyDataText = "No data available";
                    gridMostPopularAuthor.DataBind();
                }
   
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int month;
            int year;
            if (DropDownList1.SelectedIndex != 0 && DropDownList2.SelectedIndex != 0)
            {             
                month = Convert.ToInt32(DropDownList1.SelectedValue);
                year = Convert.ToInt32(DropDownList2.SelectedValue);
                if (authorSelected == false)
                {
                    visibility(false, true, false);
                    gridMostPopularBook.DataSource = MostPopularBL.popularBookList(month, year);
                    gridMostPopularBook.EmptyDataText = "No data available";
                    gridMostPopularBook.DataBind();
                }
                else if (authorSelected == true)
                {
                    visibility(true, false, false);
                 
                    gridMostPopularAuthor.DataSource = MostPopularBL.popularAuthorList(month, year);
                    gridMostPopularAuthor.EmptyDataText = "No data available";
                    gridMostPopularAuthor.DataBind();
                }
   
            
            }

        }

        protected void gridMostPopularBook_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            int month = Convert.ToInt32(DropDownList1.SelectedValue);
            int year = Convert.ToInt32(DropDownList2.SelectedValue);
            gridMostPopularBook.PageIndex = e.NewPageIndex;
            gridMostPopularBook.DataSource = MostPopularBL.popularBookList(month, year);
            gridMostPopularBook.DataBind();
        }

        protected void gridMostPopularAuthor_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            int month = Convert.ToInt32(DropDownList1.SelectedValue);
            int year = Convert.ToInt32(DropDownList2.SelectedValue);
            gridMostPopularAuthor.PageIndex = e.NewPageIndex;
            gridMostPopularAuthor.DataSource = MostPopularBL.popularAuthorList(month, year);
            gridMostPopularAuthor.DataBind();
        }
    }
}
